#!/usr/bin/env python


import os, shutil
#assume that the patched kernel and the custom ramdisk that needs to be installed inside the test device are placed in the current folder.
#method adapted from https://code.google.com/p/iphone-dataprotection/source/browse/#hg%2Framdisk_tools

locationPath = "/Users/vikas/iphone-dataprotection"
locationName = "iphone-dataprotection"

def installDependencies():
	
	#install ldid
	#http://www.saurik.com/id/8
	#for generating the SHA1 hashes that are checked by Apple's iPhoneOS kerne
	os.system('curl -O http://networkpx.googlecode.com/files/ldid')
	os.system('chmod +x ldid')
	os.system('sudo mv ldid /usr/bin/')

	#install osxfuse
	os.system('curl -O -L https://github.com/downloads/osxfuse/osxfuse/OSXFUSE-2.5.4.dmg')
	os.system('hdiutil mount OSXFUSE-2.5.4.dmg')
	os.system('sudo installer -pkg /Volumes/FUSE\ for\ OS\ X/Install\ OSXFUSE\ 2.5.pkg -target /')
	os.system('hdiutil eject /Volumes/FUSE\ for\ OS\ X/')

	
	#install python crypto modules
	os.system("sudo ARCHFLAGS='-arch i386 -arch x86_64' easy_install pycrypto")
	os.system('sudo easy_install M2crypto construct progressbar')

	#install mercurial and clone the iphone-dataprotection files from code.google.com
	os.system('hg clone https://code.google.com/p/iphone-dataprotection/')
	os.system("mv iphone-dataprotection {}".format(locationName))

	
	os.chdir('{}'.format(locationPath));
	
	os.system('make -C img3fs/')

	# #install redsnow to add our custom ramdisk into the iphone
	os.system('curl -O -L https://sites.google.com/a/iphone-dev.com/files/home/redsn0w_mac_0.9.9b8.zip')
	os.system('unzip redsn0w_mac_0.9.9b8.zip')
	
	os.system('cp redsn0w_mac_0.9.9b8/redsn0w.app/Contents/MacOS/Keys.plist .')

	#download the IPSW if its not there in a particular home directory.
	# try:
	# 	#check if its there in the home directory
	# 	src = '/Users/vikas/iPhone3,1_5.0_9A334_Restore.ipsw'
	# 	dest = locationPath

	# 	shutil.copy2(src, dest)
	# 	print("redsn0w copied")
	# except IOError:
	# 	#if not download from the internet
	# 	os.chdir("{}".format(locationPath))
	# 	os.system('curl -O -L http://appldnld.apple.com/iPhone4/041-8358.20111012.FFc34/iPhone3,1_5.0_9A334_Restore.ipsw')

	#retrieve the patched kernel from the home dir and add it to the locationPath

	try:
		#check if its there in the home directory
		src = '/Users/vikas/kernel and ram disk/kernelcache.release.n90.patched'
		dest = locationPath
		shutil.copy2(src, dest)
		print("Patched Kernel Copied")
	except IOError:
		
		print("Patched Kernel not found")

	#retrieve the patched kernel from the home dir and add it to the locationPath
	try:
		#check if its there in the home directory
		src = '/Users/vikas/kernel\ and\ ram\ disk/myramdisk_n90ap.dmg'
		dest = locationPath

		shutil.copy2(src, dest)
		print("RamDisk copied")
	except IOError:
		
		print("Ram Disk not found")
	

def loadToolKit():
	os.chdir('{}'.format((locationPath)))
	os.system('./redsn0w_mac_0.9.9b8/redsn0w.app/Contents/MacOS/redsn0w -i iPhone3,1_5.0_9A334_Restore.ipsw -r myramdisk_n90ap.dmg -k kernelcache.release.n90.patched')

# def communicateWithDevice():
# 	python usbmuxd-python-client/tcprelay.py -t 22:2222 1999:1999

# def decrypt():
# 	python python_scripts/demo_bruteforce.py

def main():
	#first install all the dependencies
	#installDependencies()
	loadToolKit()
	# communicateWithDevice()
	# decrypt()


if __name__ == '__main__':
	main()